package com.smartwebx.ha.biz.rainwater.real.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Iterables;
import com.smartwebx.ha.biz.rainwater.feature.FeatureTideManager;
import com.smartwebx.ha.biz.rainwater.real.ProcessDataManager;
import com.smartwebx.ha.biz.rainwater.real.RiverFloodControlManager;
import com.smartwebx.ha.biz.rainwater.real.TideManager;
import com.smartwebx.ha.dal.feature.dao.FeatureTideMapper;
import com.smartwebx.ha.dal.realtime.dao.TideMapper;
import com.smartwebx.ha.dal.realtime.dao.handler.ProcessDataEnum;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.FeatureHighLow;
import com.smartwebx.ha.dal.usebean.RiverFC;
import com.smartwebx.ha.dal.usebean.WaterYearComp;
import com.smartwebx.ha.dal.usebean.view.ViewDayProcessAnalysis;
import com.smartwebx.ha.dal.util.core.WaterProcessDatasAnalysis;

@Service
public class TideManagerImpl implements TideManager {

	@Autowired
	private TideMapper tideDao;
	@Autowired
	private FeatureTideMapper featureTideDao;
	@Autowired
	private ProcessDataManager processDataService;
	@Autowired
	private FeatureTideManager featureTideService;
	@Autowired
	private RiverFloodControlManager riverFloodControlService;

	@Override
	public List<DateValue> getSingleStatHourProcess(String stcd,
			Date startHour, Date endHour) {
		return WaterProcessDatasAnalysis.getSingleStatHourProcess(tideDao,
				stcd, startHour, endHour, "water");
	}

	@Override
	public List<DateValue> getSingleStatWaterDayProcess(String stcd,
			Date startDate, Date endDate) {

		return WaterProcessDatasAnalysis.getSingleStatDayProcess(tideDao, stcd,
				startDate, endDate, "water");
	}

	@Override
	public List<DateValue> getPurenessWaterProcesss(String stcd,
			Date startDate, Date endDate) {
		return WaterProcessDatasAnalysis.getPurenessProcesss(tideDao, stcd,
				startDate, endDate, "water");
	}

	@Override
	public WaterYearComp getMoreYearWaterCompSotr(String stcd, Date startDate,
			Date endDate) {
		Iterable<DateValue> maxMinAvg = processDataService
				.getWaterProcessMaxMinAvg(startDate, endDate,
						ProcessDataEnum.CHAO_WEI, stcd);
		return WaterProcessDatasAnalysis.getMoreYearCompSort(featureTideDao,
				stcd, maxMinAvg, "water");
	}

	@Override
	public ViewDayProcessAnalysis getDayProcessAnalysis(String stcd, Date bTm,
			Date eTm) {
		// 最高最低水位
		FeatureHighLow hisHighLowWater=featureTideService.getMoreYearStatHighLowWater(stcd);
		//潮位过程最高最低平均
		Iterable<DateValue> waterMaxMinAvg=processDataService.getWaterProcessMaxMinAvg(bTm, eTm, ProcessDataEnum.CHAO_WEI, stcd);
		//防洪信息
		RiverFC riverFC=riverFloodControlService.getSingleRiverFloodControl(stcd);
		int exceedWarnDay = 0;
		if (riverFC != null) {
			exceedWarnDay = processDataService.getWaterExceedWarnDays(
					bTm, eTm, ProcessDataEnum.CHAO_WEI, stcd,
					riverFC.getWarWater());
		}
		WaterYearComp waterYearComp=this.getMoreYearWaterCompSotr(stcd, bTm, eTm);
		ViewDayProcessAnalysis viewDayProcessAnalysis= new ViewDayProcessAnalysis();
		// 历史最高水位
		viewDayProcessAnalysis.setHisMaxWater(new DateValue(hisHighLowWater.getMaxzTm(),hisHighLowWater.getMaxz()));
		// 历史最低水位
		viewDayProcessAnalysis.setHisMinWater(new DateValue(hisHighLowWater.getMinzTm(),hisHighLowWater.getMinz()));
		// 最高水位
		viewDayProcessAnalysis.setMaxWater(Iterables.get(waterMaxMinAvg, 0));
		//最低水位
		viewDayProcessAnalysis.setMinWater(Iterables.get(waterMaxMinAvg, 1));
		//最高水位排序
		viewDayProcessAnalysis.setMaxWaterSort(waterYearComp.getMaxSotrHistory());
		//最低水位排序
		viewDayProcessAnalysis.setMinWaterSort(waterYearComp.getMinSotrHistory());
		//最大值序列
		viewDayProcessAnalysis.setWaterMaxs(waterYearComp.getMaxHisArray());
		//最小值序列
		viewDayProcessAnalysis.setWaterMins(waterYearComp.getMinHisArray());
		//超警天数
		viewDayProcessAnalysis.setWarDayNum(exceedWarnDay);
		
		return viewDayProcessAnalysis;
	}

}
